package com.dragon.app.controller.business;

import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.IContractArticleService;
import com.dragon.repository.domain.dto.ContractArticleDTO;
import com.dragon.repository.domain.vo.ContractArticleVO;
import com.dragon.repository.entity.ContractArticle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 条款 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-21
 */
@RestController
@RequestMapping("contractArticle")
@Api(tags = "条款")
public class ContractArticleController {
    
    @Autowired
    IContractArticleService service;

    @GetMapping("list")
    @ApiOperation("条款列表")
    public ResponseData<List<ContractArticleDTO>> articleList() {
        List<ContractArticle> contractArticles = service.articleList();
        return ResponseData.success(ContractArticleDTO.convertList(contractArticles));
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public ResponseData articleUpdate(@RequestBody @Validated(Update.class) ContractArticleVO vo){
        service.updateArticle(ContractArticleVO.convert(vo));
        return ResponseData.success();
    }


    @PostMapping("oneDetail")
    @ApiOperation("查看某个详情")
    public ResponseData<ContractArticleDTO> viewOneDetail(@ApiParam(name = "articleId",value = "条款id", required = true, type = "int")
                                                          @RequestParam Integer articleId ){
        ContractArticle article = service.viewOneDetail(articleId);
        return ResponseData.success(ContractArticleDTO.convert(article));
    }

    @GetMapping("defaultArticle")
    @ApiOperation(value = "获取默认条款")
    public ResponseData<ContractArticleDTO> getDefaultArticle(){
        ContractArticle defaultArticle = service.getDefaultArticle();
        return ResponseData.success(ContractArticleDTO.convert(defaultArticle));
    }

}
